/*
 * Album_test.cpp
 *
 *  Created on: Jun 22, 2013
 *      Author: amatzlia
 */

#include "Album.h"
#include "mtmtest.h"
#include "list.h"
#include <set>
using namespace mtm;
using std::set;

namespace AlbumTest {

void createTest() {
	ASSERT_THROWS(INVALID_BARCODE, Album(0));
	ASSERT_THROWS(INVALID_BARCODE, Album(-1));
	ASSERT_THROWS(INVALID_BARCODE, Album(0, 0, 0, 0, "", ""));
	ASSERT_THROWS(INVALID_BARCODE, Album(-1, 0, 0, 0, "", ""));
	ASSERT_THROWS(INVALID_BARCODE, Album(0, 0, 0, 0, "", ""));
	ASSERT_THROWS(INVALID_PRICE, Album(1, 0, 0, 0, "", ""));
	ASSERT_THROWS(INVALID_PRICE, Album(1, -1, 0, 0, "", ""));
	ASSERT_THROWS(INVALID_NUM_PURCHASED, Album(1, 1, 0, -1, "", ""));
	ASSERT_EQUALS(getAlbumFullString(10, 20, 30, 40, "title2", "artist2"),
			Album(10, 20, 30, 40, "title2", "artist2").getFullString());
}

void getStringTest() {
	Album album(1, 2, 3, 4, "title1", "artist1");
	ASSERT_EQUALS(getAlbumFullString(1, 2, 3, 4, "title1", "artist1"),
			album.getFullString());
	ASSERT_EQUALS(getAlbumMinimalString("title1", "artist1", 3, 2),
			album.getMinimalString());
}

void purchaseTest() {
	Album album(1, 2, 3, 4, "title1", "artist1");
	int price = album.purchase();
	ASSERT_EQUALS(2, price);
	price = album.purchase();
	ASSERT_EQUALS(2, price);
	ASSERT_EQUALS(getAlbumFullString(1, 2, 3, 6, "title1", "artist1"),
			album.getFullString());
}

void sortByBarcodeTest() {
	set<Album, Album::SortByBarcode> albums;
	albums.insert(Album(1));
	albums.insert(Album(10));
	albums.insert(Album(2));
	albums.insert(Album(7));
	int sortedBarcodes[] = { 1, 2, 7, 10 };
	int i = 0;
	for (const Album& album : albums) {
		ASSERT_EQUALS(Album(sortedBarcodes[i++]).getFullString(),
				album.getFullString());
	}
}

void barcodeEqualsTest() {
	List<Album> albums;
	albums.insert(Album(1));
	albums.insert(Album(10));
	albums.insert(Album(2));
	albums.insert(Album(7));

	auto iterator = albums.find(Album::BarcodeEquals(Album(2)));
	ASSERT_EQUALS(Album(2).getFullString(), (*iterator).getFullString());
	iterator = albums.find(Album::BarcodeEquals(Album(100)));
	ASSERT_TRUE(iterator == albums.end());
}

void sortByNumPurchasedThenBarcodeTest() {
	set<Album, Album::SortByNumPurchasedThenBarcode> albumSet;
	Album albums[] = { Album(7, 1, 1, 20, "", ""), Album(10, 1, 1, 20, "", ""),
			Album(1, 1, 1, 10, "", ""), Album(2, 1, 1, 10, "", "") };
	albumSet.insert(albums[3]);
	albumSet.insert(albums[1]);
	albumSet.insert(albums[0]);
	albumSet.insert(albums[2]);

	int i = 0;
	for (const Album& album : albumSet) {
		ASSERT_EQUALS(albums[i++], album);
	}
}

void comparisonTest() {
	Album album1(10, 1, 1, 1, "", ""), album2(12, 1, 1, 1, "", ""), album3(10,
			2, 3, 4, "bla", "boo");
	ASSERT_TRUE(album1 == album3);
	ASSERT_TRUE(album3 == album1);
	ASSERT_FALSE(album1 == album2);
	ASSERT_FALSE(album2 == album3);
}

}

/* uncomment to run tests, or copy the relevant code */
//using namespace AlbumTest;
//int main() {
//	RUN_TEST(createTest);
//	RUN_TEST(getStringTest);
//	RUN_TEST(purchaseTest);
//	RUN_TEST(sortByBarcodeTest);
//	RUN_TEST(barcodeEqualsTest);
//	RUN_TEST(sortByNumPurchasedThenBarcodeTest);
//	RUN_TEST(comparisonTest);
//	return 0;
//}
